package com.kitty.game.equip.handler;

import com.kitty.game.enter.FiedValue;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.config.Equip;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.utils.AsktaoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Map;

import static com.kitty.game.utils.Const.*;
import static com.kitty.game.utils.NotifyModules.NOTIFY_UPGRADE_JEWELRY_OK;
import com.kitty.game.equip.handler.UpgradeHandler;

@Component("JewelryAppointHandler")
@Slf4j
public class JewelryAppointHandler extends UpgradeHandler {

    private static final String ERROR_RESOURCES = "请放入正确的装备或材料。";
    private static final String STONE = "超级女娲石";
    private static final String SUCCESS = "恭喜你，花费了{0}文钱，成功合成了1件#R{1}#n。";
    /**
     * 项链
     */
    private static final String NECKLACE_NAME = "雪魂丝链";
    /**
     * 玉佩
     */
    private static final String BALDRIC_NAME = "蟠螭结";
    /**
     * 手镯
     */
    private static final String WRIST_NAME = "法文手轮";


    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip roleEquip = (RoleEquip) params[0];
        Equip upgradeEquip = (Equip) params[1];
        boolean isUseLimit = (boolean) params[2];
        String needJewelryName = (String) params[3];

        int needCash = needCash(upgradeEquip.getReq_level(), 0);
        roleService.subtractMoney(role, needCash);
        int limitNum = 0;
        /**大于100级才需要消耗2个70首饰*/
        if (upgradeEquip.getReq_level() > 100) {
            limitNum = equipService.delRoloEquip(needJewelryName, 2, isUseLimit, role);
        }else {
            limitNum += equipService.delRoloEquip(STONE, 2, isUseLimit, role);
        }
        if (roleEquip.isEverLimit()) {
            limitNum++;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("oldname=="+roleEquip.getName());
        stringBuilder.append("，");
        boolean boo = false;
        for (Map.Entry<Short, FiedValue> entry:roleEquip.getFields().get((short) 514).getField().entrySet()){
            int type = (int) entry.getValue().getType();
            int value = (int) entry.getValue().getValue();
            if ((type == 221&&value==5)){
                boo = true;
                stringBuilder.append("相5=="+boo);
                stringBuilder.append("，");
                break;
            }
        }
        roleEquip = equipService.getHighJewelry(upgradeEquip, "金色", role, limitNum, roleEquip);
        log.error("高级合成首饰=={}=={}=={}=={}",role.getRoleId(),role.getName(),stringBuilder.toString(),roleEquip.getName());
        refreshRoleEquip(role, roleEquip);
        pushOperateResult(role, UPGRADE_SUCCESS);
        return MessageFormat.format(SUCCESS, AsktaoUtil.getMoneyFormat(needCash), roleEquip.getName());
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, upgradePos);
        if(roleEquip==null){
            return "请放入正确的装备。";
        }
        boolean isUseLimit = param.equals("1");
        isUseLimit = true;
        if (!isRightPosition(roleEquip.queryEquipPos(false))) {
            return ERROR_RESOURCES;
        }
        Equip equip = EquipDataPool.getByName(roleEquip.getName());
        if (equip == null) {
            log.error("找不到装备配置=[{}]", roleEquip.getName());
            return ERROR_RESOURCES;
        }
        Equip upgradeEquip = EquipDataPool.getByName(equip.getUpgradeName());
        if (upgradeEquip == null) {
            log.error("找不到装备配置=[{}]", equip.getKey_name());
            return ERROR_RESOURCES;
        }
        String needJewelryName;
        if (upgradeEquip.getPosition() == NECKLACE) {
            needJewelryName = NECKLACE_NAME;
        } else if (upgradeEquip.getPosition() == BALDRIC) {
            needJewelryName = BALDRIC_NAME;
        } else if (upgradeEquip.getPosition() == LEFT_WRIST) {
            needJewelryName = WRIST_NAME;
        } else {
            return ERROR_OPTYPE;
        }
        String content = checkResource(role, upgradeEquip, isUseLimit, needJewelryName);
        if (content != null) {
            return content;
        }

        return doUpgrade(role, roleEquip, upgradeEquip, isUseLimit, needJewelryName);
    }

    @Override
    public String checkResource(Object... objects) {
        Role role = (Role) objects[0];
        Equip upgradeEquip = (Equip) objects[1];
        boolean isUseLimit = (boolean) objects[2];
        String needJewelryName = (String) objects[3];
        int stoneNum = equipService.getCount(STONE, isUseLimit, role);
        if (upgradeEquip.getReq_level() > 100) {
            int reqJewelryNum = equipService.getCount(needJewelryName, isUseLimit, role);
            if (reqJewelryNum < 2) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, needJewelryName);
            }
        }else {
            if (stoneNum < 2) {
                return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, STONE);
            }
        }
        if (role.getMoney() < needCash(upgradeEquip.getReq_level(), 0)) {
            return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, MONEY_NAME);
        }
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        if (level <= 100) {
            return level * 6000;
        } else {
            return level * 10000;
        }
    }

    @Override
    public String getUpdateTypeStr() {
        return "jewelry_appoint";
    }

    @Override
    int getNotifyType() {
        return NOTIFY_UPGRADE_JEWELRY_OK;
    }

    /**
     * 判断打造的物品装备的部位是否符合条件
     */
    @Override
    boolean isRightPosition(byte equipPos) {
        return equipPos == NECKLACE || equipPos == BALDRIC || equipPos == LEFT_WRIST;
    }
}
